$('#userBank').click(function(){
	var frm = document.forms['userBank'];
	var bank_num = frm.elements['bank_num'].value;
	var bank_name = frm.elements['bank_name'].value;
	var bank_tel = frm.elements['bank_tel'].value;
	var province = frm.elements['province'].value;
	var city = frm.elements['city'].value;

	if(!bank_num){msgs('请填写银行卡号');return false;}
	if(!bank_name){msgs('请填写银行卡类型');return false;}
	if(!bank_tel){msgs('请填写银行卡预留手机');return false;}
	if(!province){msgs('请选择省份');return false;}
	if(!city){msgs('请选择城市');return false;}
	$('#userBanks').submit();

});



function ajax_bankcardinfo(obj){
	var val  = obj.value;
	if(!val){
		return false;
	}
	Ajax.call('user.php?act=ajax_bankcardinfo', 'val=' + val, ajax_bankcardinfos, 'POST', 'JSON');
}
function ajax_bankcardinfos(data){
	 
	$('input[name=bank_name]').val(data.msg);
}

$('.pay-style-line').click(function(){
	$(this).siblings('.pay-style-line').find('i').removeClass('check-cur');
	$(this).find('i').addClass('check-cur');
	var code = $(this).find('i').attr('pay_code');
	$('#pay_code').val(code);
	
	if(code != 'balance'){
		$('#top-up-money').hide();
	}else{
		$('#top-up-money').show();
	}
});

 
$('.pay-btn').click(function(event){
	$(this).attr('disabled',"true");//添加disabled属性

	var code = $('#pay_code').val();
	var pay_password = $('#pay_password').val();
	var cart_id = $('#cart_id').val();
	if(code == 'balance' && !pay_password){
		msgs('请填写青山羊币支付密码');
		$('.pay-btn').removeAttr("disabled"); //移除disabled属性
		return false;
	}
	var arr        = new Object();
	arr.code = code;
	arr.pay_password = pay_password;
	arr.cart_id = cart_id;
	Ajax.call('sheep.php?act=done', 'arr=' + $.toJSON(arr), maiyangdone, 'POST', 'JSON');
});

/* 直接支付订单  */
$('#pay_zhifuswww').click(function(event){
	$(this).attr('disabled',"true");//添加disabled属性

	var code = $('#pay_code').val();
	var pay_password = $('#pay_password').val();
	var order_id = $('#order_id').val();
	if(code == 'balance' && !pay_password){
		$('#pay-btn').removeAttr("disabled"); //移除disabled属性
		$('.pay-btn').removeAttr("disabled"); //移除disabled属性
		msgs('请填写青山羊币支付密码');
		
		return false;
	}
	var arr        = new Object();
	arr.code = code;
	arr.pay_password = pay_password;
	arr.order_id = order_id;
	Ajax.call('sheep.php?act=dones', 'arr=' + $.toJSON(arr), maiyangdone, 'POST', 'JSON');
});
function maiyangdone(res){
	$('.pay-btn').removeAttr("disabled"); //移除disabled属性
	if(res.error == 0){
		msgs(res.message);
		setTimeout(function(){window.location.href='sheep.php?act=pay_result&id='+ res.order_id},2500);
		return false;
	}else if(res.error == 1){
		msgs(res.message);
		setTimeout(function(){window.location.href='user.php'},2500);
		return false;
	}else if(res.error == 2){
		msgs(res.message);
		setTimeout(function(){window.location.href='sheep.php'},2500);
		return false;
	}
	/* 支付宝支付 */
	else if(res.error == 10){
		$('#pay_zhifu').append(res.pay_online);
		$('#pay_zhifus').hide();
		// $('#pay_zhifu').append(res.pay_online);
		// $('#pay_zhifus').css('display':'none');
		// $('#pay_zhifu').append(res.pay_online);
		// $(document).on('#pay_zhifu input','click',function(){

		// })
		// $('#pay_zhifu input').click();
		return false;
	}
	/* 微信H5 支付 */
	else if(res.error == 20){
		if(res.message=="success") {
			pay(res.appId2,
				res.timeStamp,
				res.nonceStr2,
				res.packages,
				res.signType2,
				res.paySign2,
				res.order_id
				);
		}
	}
}



function pay(appId, timeStamp, nonceStr, package1, signType, paySign,order_id) {

	WeixinJSBridge.invoke('getBrandWCPayRequest', {
		'appId' : String(appId), //公众号名称，由商户传入
		'timeStamp' : String(timeStamp), //时间戳，自 1970 年以来的秒数
		'nonceStr' : String(nonceStr), //随机串
		'package' : String(package1),
		'signType' : String(signType), //微信签名方式
		'paySign' : String(paySign) //微信签名

	}, function(res) {
 
	if(res.err_msg == "get_brand_wcpay_request:ok"){  
			msgs("支付成功");  
			ajax_gengxin_yang(order_id);
		WeixinJSBridge.call('closeWindow'); 
			
		 // window.location.href='http://shop.zunbaoge.com/mobile/distribute.php'; //mai 支付成功返回地址
	   }
		else if(res.err_msg == "get_brand_wcpay_request:cancel"){  
			msgs("您取消了支付。"); 
			setTimeout(function(){window.location.href='user.php'},2500);			
	   }
		else{  
			msgs("支付失败!");  
			setTimeout(function(){window.location.href='user.php'},2500);
	   }  
	// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg 将在用户支付成功后返回 ok，但并不保证它绝对可靠。
	});
}

function ajax_gengxin_yang(order_id){
	var arr        = new Object();
	arr.order_id = order_id;
	Ajax.call('sheep.php?act=ajax_gengxin', 'arr=' + $.toJSON(arr), maiyangdone, 'POST', 'JSON');	
}












$('#tixian_pay').click(function(){
	var bank_id = $('#bank_id').val();
	var user_money = parseInt($('#user_money').val());
	var password = $('#password').val();
	var code = $('#code').val();
	var tixian_money = parseInt($('#tixian_money').val());
	if(!bank_id){
		msgs("没有绑定银行卡,进行绑定");
		setTimeout(function(){window.location.href='user.php?act=mybankcard'},3000);
		return false;
	}
	if(!user_money){
		msgs("请填写提现金额");
		return false;
	}	
	if(tixian_money > user_money){
		msgs("最低提现金额"+tixian_money);
		return false;
	}
	if(!password || password.length < 6){
		msgs("请输入支付密码，密码长度最少6位");
		return false;
	}
	if(!code){
		msgs("验证码不能为空");
		return false;
	}
	$('#formSurplus').submit();
});


$('#yufumoney').click(function(){
	var amount = $('#amount').val();
	var user_note = $('#user_note').val();
	if(!amount){
		msgs("请填写充值金额");
		return false;
	}
	if(!user_note){
		msgs("请填写凭证号");
		return false;
	}
	$('#formSurplus').submit();
});

$('.xingxingBox li').click(function(){
	var index = $(this).index();
	
	$('.xingxingBox li').removeClass('cur');
	for(i=0;i<=index;i++){
		var index1 = i + 1;
		$('#leixing').val(index1);
		$('.xingxing'+index1).addClass('cur');
	}
 
});